<?php
	defined('_JEXEC') or die('Restricted access');	
	$subtotal = 0;
	JHtml::_('jquery.framework');
	$doc = JFactory::getDocument();
	$doc->addScript(JUri::root(true)."/media/com_bakerys/js/function.js");
	$doc->addScript(JUri::root(true)."/media/com_bakerys/js/php.js");
	$doc->addScript(JUri::root(true)."/media/com_bakerys/js/bakery.js");
	
	$input = JFactory::getApplication()->input;
	$location_id = $input->get("location");
?>
<form action="index.php" method="post" >
<div class="cart-view">
	<div>
		<h1><?php echo JText::_("COM_BAKERYS_CART_TITLE") ?></h1>
	</div>
	<div>
		<table id='table_cart' class="table table-striped table-bordered">
			<thead>
				<tr>
					<th><?php echo JText::_("COM_BAKERYS_CART_PRODUCTNAME") ?></th>
					<th><?php echo JText::_("COM_BAKERYS_CART_PRODUCTPRICE") ?></th>
					<th><?php echo JText::_("COM_BAKERYS_CART_PRODUCTQUANTITY") ?></th>
					<th><?php echo JText::_("COM_BAKERYS_CART_PRODUCTAMOUNT") ?></th>
					<th><?php echo JText::_("COM_BAKERYS_CART_LINKREMOVE") ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($this->cart as $item): ?>
					<tr>
						<td>
							<div class="PricesalesPrice" style="display:block !important">
								<span class="PricesalesPrice">
									<?php echo $item["productname"] ?>
								</span>
							</div>
						</td>
						<td>
							<div class="text-right PricesalesPrice" style="display:block !important">
								<span class="PricesalesPrice">
									<?php echo number_format($item["price"],2,".",",") ?> $
								</span>
							</div>
							<input type='hidden' value='<?php echo $item["price"] ?>' class='price_hidden' />
						</td>
						<td width="20px">
							<input class="span12 product_quantity" productid='<?php echo $item["productid"] ?>' type="text" value="<?php echo $item["quantity"] ?>" />
						</td>
						<?php 
							$amount = $item["price"] * $item["quantity"]; 
							$subtotal += $amount;
						?>
						<td>
							<div class="text-right PricesalesPrice" style="display:block !important">
								<span class="PricesalesPrice product_amount">
									<?php echo number_format($amount,2,".",",") ?> $
								</span>
							</div>
							<input type='hidden' value='<?php echo $amount ?>' class='amount_hidden' />
						</td>
						<td>
							<span class="PricesalesPrice">
								<a style="cursor:pointer" onclick="removecart(<?php echo $item["productid"]?>,<?php echo $location_id?>)" ><?php echo JText::_("COM_BAKERYS_CART_LINKREMOVE") ?></a>
							</span>							
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="4">
						<div class="text-right PricesalesPrice" style="display:block !important">
							<?php echo JText::_("COM_BAKERYS_CART_TOTAL") ?>
							<span class="PricesalesPrice" id="total_cart_hidden">
								<?php echo number_format($subtotal,2,".",",") ?> $
							</span>
						</div>
						
					</td>
					<td></td>
				</tr>				
			</tfoot>			
		</table>
		<input type="button" onclick="updatecart(<?php echo $location_id?>)" class="btn btn-primary"  value="Check out">
	</div>
</div>
<input type="hidden" value="com_bakerys" name="option" />
<input type="hidden" value="cart.checkout" name="task" />
</form>
<script>
	jQuery(document).ready(function(){
		jQuery("input.product_quantity").input_number();
		jQuery("input.product_quantity").bind("keyup",function(){
			var value = jQuery(this).val();
			changeQuantity(value,this);
		})
	})
	
	function changeQuantity(quantity,el)
	{
		var price = jQuery(el).closest("tr").find(".price_hidden").val(); 
		var amount = parseFloat(price) * parseInt(quantity);
		jQuery(el).closest("tr").find(".product_amount").text(number_format(amount,2,".",",") + " $");
		if(jQuery.trim(quantity) != "")
			jQuery(el).closest("tr").find(".amount_hidden").val(amount);
		else jQuery(el).closest("tr").find(".amount_hidden").val(0);
		updateTotal();
	}

	function updateTotal()
	{
		var total = 0;
		jQuery("#table_cart tbody tr").each(function(){
			total += parseFloat(jQuery(this).find(".amount_hidden").val());
		})
		jQuery("#total_cart_hidden").text(number_format(total,2,".",",") + " $");
	}
</script>